package com.uenpay.im.ui.old.activity.company;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
/**
 * @author SL
 * @created 2016-2-22 13:52:22
 * @copyRight http://www.qiyunxin.com
 */
public class GetDepartByPidListInvokeItem extends BaseHttpInvokeItem {

	public class GetDepartByPidListInvokeItemResult extends HttpInvokeResult {
		public ArrayList<DepartmentEntity> arrayList = new ArrayList<DepartmentEntity>();
	}

	public GetDepartByPidListInvokeItemResult getOutPut() {
		return (GetDepartByPidListInvokeItemResult) GetResultObject();
	}

	public GetDepartByPidListInvokeItem(int pid, int entid) {
		String url = APIConfiguration.getBaseUrl()
				+ "Organization/Organization/getDepartByPid?pid=" + pid
				+ "&entid=" + entid + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetDepartByPidListInvokeItemResult result = new GetDepartByPidListInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");

			JSONArray jsonarr = json.optJSONArray("data");
			if (jsonarr != null) {
				for (int i = 0, length = jsonarr.length(); i < length; i++) {
					result.arrayList.add(desDepartmentEntity(jsonarr
							.optJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

	private DepartmentEntity desDepartmentEntity(JSONObject json) {
		DepartmentEntity mDepartmentEntity = new DepartmentEntity();
		mDepartmentEntity.pid = json.optInt("pid");
		mDepartmentEntity.isleaf = json.optInt("isleaf");
		mDepartmentEntity.custnum = json.optInt("custnum");
		mDepartmentEntity.departid = json.optInt("departid");
		mDepartmentEntity.entid = json.optInt("entid");
		mDepartmentEntity.departname = json.optString("departname");
		mDepartmentEntity.pdepartname = json.optString("pdepartname");
		return mDepartmentEntity;
	}

}
